/**
 * Created by Administrator on 2018/8/17.
 */

var $$scope;
var $CertusFormAjax;
var $CertusJsonAjax;
var $fileListService;
// 资产库可用资产信息
var purchaseProductDatas = new Array();
var bqArray = {};
var purchaseComparisionStepEntity = {}
var proc1Columns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'name',
        title: '名称',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'descAndSpec',
        title: '品名及规格',
        width: 200,
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'quantity',
        title: '数量',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'singlePrice',
        title: '单价',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
        }
    }, {
        id: 'estimatedAmount',
        title: '预计金额',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
        }
    }, {
        id: 'remark',
        title: '备注',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'operation',
        title: '操作',
        width: 220,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-primary" onclick="shadbox(\'' + record.biCode + '\',\'' + record.commodityClassCode + '\');"><i class="icon-eye-open"></i>  报价历史库</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-success" onclick="showNowQuote(\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  报价</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-info" onclick="shpriceox(\'' + record.biCode + '\')"><i class="icon-plus"></i>  报价</button>';
            return content;
        }
    }];
// 供应商报价列表
var prcListColumns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 74,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 74,
        type: 'string',
        columnClass: 'text-center',
    }
    , {
        id: 'supplierName',
        title: '供应商名称',
        // width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'supplierPhone',
        title: '联系方式',
        width: 110,
        type: 'string',
        columnClass: 'text-center',
    },
    {
        id: 'specification',
        title: '规格(参数)',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'price',
        title: '价格(说明)',
        // width: 120,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'priceIncludingTax',
        title: '价格(含税)',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
        }
    }, {
        id: 'dateOfDelivery',
        title: '交货期',
        width: 80,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'guaranteeTime',
        title: '质保期',
        width: 74,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'paymentMethod',
        title: '付款方式',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'strengthsAndWeaknesses',
        title: '优劣势',
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'operation',
        title: '操作',
        width: 240,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-info" onclick="shproc(\'' + record.bqCode + '\',\'' + record.biCode + '\');"><i class="fa fa-edit"></i>  编辑</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-success" onclick="shupload(\'' + record.bqCode + '\',\'' + record.biCode + '\');"><i class="icon-cloud-upload"></i>  附件</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-primary" onclick="openDownloadList(\'' + record.bqCode + '\',\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  附件</button>';
            content += '  ';
            content += '<button class="btn btn-xs btn-danger" onclick="del(\'' + record.bqCode + '\',\'' + record.biCode + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }
];
var picker2 = $('#dateOfDeliveryId').datetimepicker({
    format: 'YYYY-MM-DD',
    locale: moment.locale('zh-cn')
}).on('dp.change', function (e) {
    ;
    var result = new moment(e.date).format('YYYY-MM-DD');
    if (result && !angular.isUndefined(result)) {
        $$scope.buyingQuoteItem.dateOfDelivery = result;
        //console.log(result);
        // $scope.$apply();
    }
})
var picker3 = $('#guaranteePeriodId').datetimepicker({
    format: 'YYYY-MM-DD',
    locale: moment.locale('zh-cn')
}).on('dp.change', function (e) {
    ;
    var result = new moment(e.date).format('YYYY-MM-DD');
    if (result && !angular.isUndefined(result)) {
        $$scope.buyingQuoteItem.guaranteeTime = result;
        //console.log(result);
        // $scope.$apply();
    }
})
var fileInstantiation = {
    classCateCode: 'WBLY',
    fileClassCode: 'KHBJD'
}
var fileInstantiationDisable = {
    classCateCodeDis: true, //文件类目不可修改
    fileClassCodeDis: true //文件类型不可修改
}
var pricelogColumns = [
    {
        id: 'commodityCateName',
        title: '类目',
        width: 74,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'commodityClassName',
        title: '类别',
        width: 74,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'name',
        title: '名称',
        width: 74,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'address',
        title: '地点',
        width: 74,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'supplierName',
        title: '供应商名称',
        // width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'supplierPhone',
        title: '联系方式',
        width: 110,
        type: 'string',
        columnClass: 'text-center',
    },
    {
        id: 'specification',
        title: '规格(参数)',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'price',
        title: '价格(说明)',
        // width: 120,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'priceIncludingTax',
        title: '价格(含税)',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '￥' + value + '元';
            return content;
        }
    }, {
        id: 'dateOfDelivery',
        title: '交货期',
        width: 80,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'guaranteeTime',
        title: '质保期',
        width: 74,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'paymentMethod',
        title: '付款方式',
        width: 100,
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'strengthsAndWeaknesses',
        title: '优劣势',
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs',
    },
    {
        id: 'operation',
        title: '操作',
        width: 60,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-primary" onclick="openDownloadList(\'' + record.bqCode + '\',\'' + record.biCode + '\')"><i class="icon-eye-open"></i>  附件</button>';
            return content;
        }
    }];
var nowBqCode;
var belowGrid;
var middleGrid;
var aboveGrid;
var brCode;


function showNowQuote(biCode) {
    if (biCode) {
        belowGrid.exhibitDatas = bqArray[biCode];
        belowGrid.refresh(false);
    }
}

//打开 历史 报价
function showAllQuote() {

    var tempArray = new Array();
    if (bqArray) {
        for (var j in bqArray) {
            tempArray = tempArray.concat(bqArray[j]);
        }
    }
    belowGrid.exhibitDatas = tempArray;
    belowGrid.refresh(false);
}

function newQuote(biCode) {

}

//打开 下载 列表
function openDownloadList(bqCode, biCode) {
    $$scope.modalContent = {};
    if (biCode && bqCode) {
        var nowBq = bqArray[biCode];
        if (nowBq) {
            for (var i in nowBq) {
                if (bqCode == nowBq[i].bqCode) {
                    if (nowBq[i].filePathArray) {
                        $$scope.modalContent = nowBq[i].filePathArray
                    } else if (nowBq[i].supplierFile) {
                        $$scope.modalContent = eval(nowBq[i].supplierFile);
                    }
                }

            }
        }
        $$scope.$apply();
    }
    // $$scope.modalContent = data.data;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = $$scope.modalContent;

    $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}

function hidefile() {
    $(".hdupload").hide();
}


function hdgs() {
    $(".shpriceox").hide();
}
// 历史报价库

function shproc(bqCode, brCode) {
    if (bqCode && brCode) {
        var bqArray2 = bqArray[brCode];
        for (var i in bqArray2) {
            if (bqArray2[i].bqCode == bqCode) {
                $$scope.buyingQuoteItem = bqArray2[i];
                $$scope.$apply();
                $(".hdgs").show();
                return;
            }
        }
    }
}

function isDifferent(element) {
    return element.bqCode == nowBqCode;
}

function del(bqCode, brCode) {
    layer.confirm('是否确认删除？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        if (bqCode && brCode) {
            var bqArray2 = bqArray[brCode];
            bqArray2 = bqArray2.filter(isDifferent);
            nowBqCode = bqCode;
            bqArray[brCode] = bqArray2;
            belowGrid.exhibitDatas = bqArray[brCode];
            belowGrid.refresh(false);
        }
        layer.msg("删除成功");
    })
}

//文件列表下载
function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}
